﻿using furion.blog.Domain.IRepositories;
using furion.blog.Domain.MySQL;
using furion.blog.Domain.Query.Message;
using furion.blog.Domain.Shared;
using furion.blog.Domain.Shared.Enum;
using Furion.DependencyInjection;
using Masuit.Tools.DateTimeExt;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace furion.blog.EntityFramework.Core.Repositories
{
    public class MessageRepository : BaseSqlSugarRepository<Message>, IMessageRepository, ISingleton
    {
        public readonly ISqlSugarClient _db;
        public MessageRepository(ISqlSugarClient db)
        {
            this._db = db;
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<bool> Save(Message model) {
            return await base.InsertAsync(model);
        }

        /// <summary>
        /// 批量保存
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public async Task<bool> SaveBatch(List<Message> data) { 
          return await base.InsertRangeAsync(data);
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<bool> Update(Message model) {
            return await base.UpdateAsync(model);
        }

        /// <summary>
        /// 设置已读
        /// </summary>
        /// <param name="id">信息id</param>
        /// <param name="toUser">接收人</param>
        /// <returns></returns>
        public async Task<bool> Read(long id, long toUser) {
            var now = DateTime.Now;
            return await base.UpdateAsync(it=>new Message { 
                IsOpen = YesOrNoEnum.Yes.ToString() ,
                OpenTime = now,
                Ts = now.GetTotalNanoseconds(),
            }, it =>it.MessageId == id & it.ToUser == toUser);
        }

        /// <summary>
        /// 统计数量
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public async Task<long> Count(MessageDetailQuery query) {
            return await base.CountAsync(query.Exp());
        }


        /// <summary>
        /// 信息分页
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public async Task<PageVO> List(MessageListQuery query) {
            try
            {
                var (strWhere, sqlParams) = query.QueryParams();
                long startIndex = query.GetStartIndex();

                string countSql = query.GetCountSQL();
                string dataSql = query.GetDataSQL();

                long total = await this._db.Ado.GetLongAsync(countSql, sqlParams);
                var data = await this._db.Ado.GetDataTableAsync(dataSql, sqlParams);
                return new PageVO { Total = total, Records = data };
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            return new PageVO { Total = 0L, Records = null };
        }


        /// <summary>
        /// 设置为已打开
        /// </summary>
        /// <param name="messageId"></param>
        /// <param name="memberId"></param>
        /// <returns></returns>
        public async Task<bool> Open(long messageId, long memberId) {
            return await base.UpdateAsync(it=>new Message { 
              IsOpen = YesOrNoEnum.Yes.ToString(),
              OpenTime = DateTime.Now
            },it=>it.MessageId == messageId & it.ToUser == memberId);
        }


        /// <summary>
        /// 通过id查找
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<Message> FindById(long id) { 
           return await base.GetByIdAsync(id);
        }


        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <param name="memberId"></param>
        /// <returns></returns>
        public async Task<bool> Del(long id, long memberId) {
            return await base.UpdateAsync(it=>new Message { 
               IsDelete = YesOrNoEnum.Yes.ToString(),
            },it=>it.MessageId == id & it.ToUser == memberId);
        }

    }
}
